﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.Common.Models;

namespace SCS.DAL
{
    public class LearnerRepository : Repository<Learner>, ILearnerRepository
    {
        public LearnerRepository(SCSContext _context)
            : base(_context)
        { }
        /// <summary>
        /// Count all Learner
        /// </summary>
        /// <returns>Total learner</returns>
        public int GetCount()
        {
            return DBSet.Count();
        }

        /// <summary>
        /// Count learner
        /// </summary>
        /// <param name="search">text search</param>
        /// <returns>Number search result</returns>
        public int GetSearchCount(string search)
        {
            return DBSet.Where(s => (s.firstName + " " + s.lastName).Contains(search) && s.deactive == false).Count();
        }

        /// <summary>
        /// Search Learner 
        /// </summary>
        /// <param name="pageNumber">pageNumber</param>
        /// <param name="pageSize">pageSize</param>
        /// <param name="search">search String</param>
        /// <returns>List Learner</returns>
        public IEnumerable<Learner> SearchLearnerPaging(int classID, string search)
        {
            if (!String.IsNullOrEmpty(search) && classID == 0)
            {
                return DBSet.Where(s => (s.firstName + " " + s.lastName).Contains(search) && s.deactive == false).OrderBy(x => x.lastName).ThenBy(t => t.firstName).ToList();
            }
            else if (String.IsNullOrEmpty(search) && classID != 0)
            {
                return DBSet.Where(s => s.classID == classID && s.deactive == false).OrderBy(x => x.lastName).ThenBy(t => t.firstName).ToList();
            }
            else
            {
                return DBSet.Where(s => (s.firstName + " " + s.lastName).Contains(search) && s.classID ==classID && s.deactive == false).OrderBy(x => x.lastName).ThenBy(t => t.firstName).ToList();
            }
        }
    }
}